<template>
  <div>
    <h2>学生管理</h2>
    <el-table :data="students" style="width: 100%">
      <el-table-column prop="id" label="学号" width="120" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="major" label="专业" />
      <el-table-column prop="class" label="班级" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button size="mini" @click="editStudent(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteStudent(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑学生弹窗 -->
    <el-dialog title="编辑学生" v-model="dialogVisible" width="400px">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="editForm.major" />
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="editForm.class" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveStudent">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const students = ref([
  { id: '20230001', name: '张三', major: '计算机', class: '1班' },
  { id: '20230002', name: '李四', major: '软件工程', class: '2班' }
])

const dialogVisible = ref(false)
const editForm = ref({ id: '', name: '', major: '', class: '' })

function editStudent(row) {
  editForm.value = { ...row }
  dialogVisible.value = true
}
function saveStudent() {
  // TODO: 调用后端接口保存
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
function deleteStudent(row) {
  // TODO: 调用后端接口删除
  students.value = students.value.filter(s => s.id !== row.id)
  ElMessage.success('删除成功')
}
</script>